package com.wxl.common.util

import android.content.Context
import android.content.SharedPreferences

/**
 * create file time : 2021/2/22
 * create user : wxl
 * subscribe :
 */
class SpUtil {

    var spXml: SharedPreferences? = null

    private object SU {
        val spUtil = SpUtil()
    }

    companion object {
        val sp = SU.spUtil
        lateinit var context: Context
    }

    private constructor()

    private fun initSpXml() {
        spXml = context.applicationContext.getSharedPreferences(
            context.packageName,
            Context.MODE_PRIVATE
        )
    }

    fun put(key: String, value: String) {
        initSpXml()
        spXml?.edit()?.putString(key, value)?.apply()
    }


    fun getString(key: String): String {
        initSpXml()
        val value = spXml?.getString(key, "")
        return value.toString()
    }

    fun putBoolean(key: String, value: Boolean) {
        initSpXml()
        spXml?.edit()?.putBoolean(key, value)?.apply()
    }

    fun getBoolean(key: String): Boolean {
        initSpXml()
        val value = spXml?.getBoolean(key, false)
        return value!!
    }


    fun putInt(key: String, value: Int) {
        initSpXml()
        spXml?.edit()?.putInt(key, value)?.apply()
    }

    fun getInt(key: String): Int {
        initSpXml()
        val value = spXml?.getInt(key, 0)
        return value!!
    }

    fun putLong(key: String, value: Long) {
        initSpXml()
        spXml?.edit()?.putLong(key, value)?.apply()
    }

    fun getLong(key: String): Long {
        initSpXml()
        val value = spXml?.getLong(key, 0)
        return value!!
    }
}